*********************************************************************************
*                                                                               *
*                				Election Frequency 						     	*
*                                                                               *
*                         		  SOE Analysis                                  *
*                                                                               *
*********************************************************************************
 

global beg_path `"C:\Users\JohnSmith\Dropbox\Replication_EF\"'  // 

cd `"${beg_path}Data\Analysis 1\"'
use electionsdata_3.dta, clear

*** Checking and rescaling coverage
fre Coverage_pop 
list Country Type Year Coverage_pop Coverage_dis if Coverage_pop ==. 
replace Coverage_pop = Coverage_pop / 100 if Country < 21 // To get shares of the population 


*\ National Elections (1st post-war & democratic) \*

***** Ranked elecetions
* creating groups
sort Country Type date

* creating dummy for election types 
gen filter_el = 1
replace filter_el = 0 if Nondem == 1
replace filter_el = 0 if Country == 5 & Year == 1964 & Type == 2 // 1964 election in Greece, not included in our study
egen Group_leg = group(Type) if Type==2&filter_el==1&date>-5202&Round==1&Coverage_pop>0.5
egen Group_ref = group(Type) if Type==4&filter_el==1&date>-5202&Round==1&Coverage_pop>0.5
egen Group_supra = group(Type) if Type==5&filter_el==1&date>-5202&Round==1&Coverage_pop>0.5
egen Group_region = group(Type) if Type==6&filter_el==1&date>-5202&Round==1&Coverage_pop>0.5
egen Group_munic = group(Type) if Type==8&filter_el==1&date>-5202&Round==1&Coverage_pop>0.5

**** Transforming stata dates to make them all positive 

*To avoid problems with negative dates (before 1960), we will add a constant equal to the minimal date in the dataset
gen posit_date = date 
su date 
replace posit_date = date+abs(r(min)) // all dates are positive

*** Generating variables with election numbers, election dates, and distance to the given election for each election type *** 
 
*Legislative election numbers 
sort Country Group_leg date 		 
quietly by Country Group_leg :  gen Rank_leg = cond(_N==1,0,_n) if Group_leg == 1
sum Rank_leg // max == 28 (Denmark)

*Variables with legislative election dates 
sort Country posit_date 		 
forvalues i=1(1)28  { 
by Country : generate Date_leg`i' = posit_date if `i' == Rank_leg
bysort Country (Date_leg`i') : gen Date_leg_`i' = Date_leg`i'[1]
drop Date_leg`i'
}

* Time to legislative election
forvalues i=1(1)28  { 
generate Timeto_leg_`i' = Date_leg_`i' - posit_date if posit_date < Date_leg_`i'
}


*Referendum numbers 
sort Country Group_ref posit_date 		 
quietly by Country Group_ref:  gen Rank_ref = cond(_N==1,0,_n) if Group_ref == 1
sum Rank_ref //   max == 23

*Variables with referendum dates 
sort Country posit_date 		 
forvalues i=1(1)23 { 
by Country : generate Date_ref`i' = posit_date if `i' == Rank_ref
bysort Country (Date_ref`i') : gen Date_ref_`i' = Date_ref`i'[1]
drop Date_ref`i'
}

* Time to referendums
forvalues i=1(1)23 { 
generate Timeto_ref_`i' = Date_ref_`i' - posit_date if posit_date < Date_ref_`i'
}


*Supranational election numbers 
sort Country Group_supra posit_date 		 
quietly by Country Group_supra:  gen Rank_supra = cond(_N==1,0,_n) if Group_supra == 1
sum Rank_supra // max == 9 

* Variables with supranational election dates
sort Country posit_date 		 
forvalues i=1(1)9 { 
by Country : generate Date_supra`i' = posit_date if `i' == Rank_supra
bysort Country (Date_supra`i') : gen Date_supra_`i' = Date_supra`i'[1]
drop Date_supra`i'
}

* Time to supranational elections 
forvalues i=1(1)9 { 
generate Timeto_supra_`i' = Date_supra_`i' - posit_date if posit_date < Date_supra_`i'
}


// Regional election numbers
sort Country Group_region posit_date 		 
quietly by Country Group_region:  gen Rank_region = cond(_N==1,0,_n) if Group_region == 1
sum Rank_region // max == 19 

* Variables with regional elections dates 
sort Country posit_date 		 
forvalues i=1(1)19 { 
by Country : generate Date_region`i' = posit_date if `i' == Rank_region
bysort Country (Date_region`i') : gen Date_region_`i' = Date_region`i'[1]
drop Date_region`i'
}

* Time to regional elections
forvalues i=1(1)19 { 
generate Timeto_region_`i' = Date_region_`i' - posit_date if posit_date < Date_region_`i'
}


// Municipal election numbers
sort Country Group_munic posit_date 		 
quietly by Country Group_munic:  gen Rank_munic = cond(_N==1,0,_n) if Group_munic == 1
su Rank_munic // max == 20 
fre Rank_munic
replace Rank_munic =  1 if Rank_munic==0

* Variables with municipal election dates 
sort Country posit_date 		 
forvalues i=1(1)20 { 
by Country : generate Date_munic`i' = posit_date if `i' == Rank_munic
bysort Country (Date_munic`i') : gen Date_munic_`i' = Date_munic`i'[1]
drop Date_munic`i'
}

* Time to municipal elections 
forvalues i=1(1)20 { 
generate Timeto_munic_`i' = Date_munic_`i' - posit_date if posit_date < Date_munic_`i'
}


********* Index of Election Frequency: FOE ************ 
 
foreach lname in  ref supra region local munic {
forvalues i = 1/28 {
cap gen no_weight_5_cont_`lname'_`i' = .
cap replace no_weight_5_cont_`lname'_`i' = Coverage_pop/exp(Timeto_`lname'_`i'/365.25) if Timeto_`lname'_`i'< 1826.25 & Timeto_`lname'_`i' > 0  &  inlist(Type, 1, 2)  
}
}


*Aggregating Contributions

foreach lname in ref supra region munic {
forvalues i = 1/28 {
egen `lname'_`i' = sum(no_weight_5_cont_`lname'_`i'), by(Country)
}
}

*Generating Indexes

foreach lname in ref supra region munic {
gen index_n_w_`lname' = .
la var index_n_w_`lname'  "IEF `lname'"
forvalues i = 1/28 {
	replace index_n_w_`lname' = `lname'_`i' if Rank_`lname' ==`i'
	}
	}

	
********* Index of Election Frequency: SOE ************ 	
	
foreach lname in leg {
forvalues i = 1/28 {
cap gen no_weight_5_cont_`lname'_`i' = .
cap replace no_weight_5_cont_`lname'_`i' = Coverage_pop/exp(Timeto_`lname'_`i'/365.25) if Timeto_`lname'_`i'< 1826.25 & Timeto_`lname'_`i' > 0 & inlist(Type, 3, 4, 5, 6, 7, 8)  
}
}


*Aggregating Contributions

foreach lname in leg {
forvalues i = 1/28 {
egen `lname'_`i' = sum(no_weight_5_cont_`lname'_`i'), by(Country)
}
}

*Generating Indexes

foreach lname in leg {
gen index_n_w_`lname' = .
la var index_n_w_`lname'  "IEF `lname'"
forvalues i = 1/28 {
	replace index_n_w_`lname' = `lname'_`i' if Rank_`lname' ==`i'
	}
	}	
	
	
**** Check **** 
bysort Type: su index_n_w_ref  

 
********* Creating one index for all election types ************ 	
egen index_n_w = rowmean(index_n_w_leg index_n_w_ref index_n_w_supra index_n_w_region index_n_w_munic) 
pwcorr index_n_w  index_n_w_leg index_n_w_ref index_n_w_supra index_n_w_region index_n_w_munic // that looks right 
 
****************
*** Figure 3 *** 
****************
 
eststo clear 
xtset Country Rank_leg
eststo M1: xtpcse Turnout index_n_w c.Year c.Year#i.Country i.Country, corr(ar1) p 
estadd local fixed "Yes" , replace 
estadd local country_year "Yes" , replace 
tsset Country Rank_munic
eststo M2: xtpcse Turnout index_n_w  c.Year c.Year#i.Country i.Country , corr(ar1) p  
estadd local fixed "Yes" , replace 
estadd local country_year "Yes" , replace 
tsset Country Rank_supra
eststo M3: xtpcse Turnout index_n_w  c.Year c.Year#i.Country i.Country , corr(ar1) p  
estadd local fixed "Yes" , replace 
estadd local country_year "Yes" , replace 
tsset Country Rank_region
eststo M4: xtpcse Turnout index_n_w   c.Year c.Year#i.Country i.Country, corr(ar1) p  
estadd local fixed "Yes" , replace 
estadd local country_year "Yes" , replace 
tsset Country Rank_ref
eststo M5: xtpcse Turnout index_n_w  c.Year c.Year#i.Country i.Country, corr(ar1) p  
estadd local fixed "Yes" , replace 
estadd local country_year "Yes" , replace 

coefplot (M1 \ M2 \ M3 \ M4 \ M5),  keep(index_n_w) aseq swapnames xlabel(-18(3)18, grid)  xline(0, lpattern(dash)) ///
ylabel(1 `""IEF: SOEs" "Turnout: Legislative""'  2 `""IEF: FOEs" "Turnout: Municipal""' ///
3 `""IEF: FOEs" "Turnout: Supranational""' 4`""IEF: FOEs" "Turnout: Regional""' 5`""IEF: FOEs" "Turnout: Referendums""') /// 
mlabel format(%9.2g) mlabposition(12) mlabgap(*2) 


cd `"${beg_path}Results"'
graph export "Figure_3.eps", replace  //


****************
*** Table A6 *** 
****************

esttab  M1 M2 M3 M4 M5 using A_Table_6.tex, one incelldelimite("\:")  stats(country_year fixed N r2, labels("Country-Specific Time Trend" "Country FE"  "N" "R2") layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" )  fmt(0 0 0 3)) replace b(2) se(2) wide staraux ///
		star(* 0.05 ** 0.01 *** 0.001) eqlabels(none) order(index_n_w) ///
		drop(*.Country*  Year ) varlabels(index_n_w "IEF" _cons "Constant",  elist(Year \hline ))   label /// 
		nodep  ///  
		nolegend note("") alignment(d{10}) /// 
		mtitles("\shortstack{\\IEF SOE\\ Turnout: Legislative}" "\shortstack{\\IEF FOE\\Turnout: Municipal}"  "\shortstack{\\IEF FOE\\Turnout: Supranational}" "\shortstack{\\IEF FOE\\Turnout: Regional}" "\shortstack{\\IEF FOE\\Turnout: Referendums}")
